angular.module('imsApp').controller('adContractsController', ['$scope', '$rootScope', '$state', 'AdContractsManager','ImsAlert','ImsDialog', function ($scope, $rootScope, $state, AdContractsManager,ImsAlert,ImsDialog) {
    $scope.search = {pageNo:$state.params.pageNo || 1,
        pageSize:$state.params.pageSize||10,
        shortName:$state.params.shortName,
        payType:$state.params.payType,
        contractType:$state.params.contractType,
        scheduleStart:$state.params.scheduleStart||null,
        scheduleEnd:$state.params.scheduleEnd||null
    };
    $scope.keyword=$scope.search.shortName;
    $scope.contractType = {42: $scope.table[42], 43: $scope.table[43] ,44: $scope.table[44]};
    $scope.payType = {45: $scope.table[45], 46: $scope.table[46], 47: $scope.table[47]};
    $scope.paginationConf = {
        currentPage: $state.params.pageNo || 1,
        itemsPerPage: $state.params.pageSize || 10,
        pagesLength: 9,
        search: 0,
        onChange: function () {
            $scope.search.pageNo = this.currentPage;
            $scope.search.pageSize = this.itemsPerPage;
            $state.go($state.current.name, $scope.search)

        }
    };
    $scope.rangeDate = {
        startDate:$scope.search.scheduleStart?moment($scope.search.scheduleStart).format('X'):null,
        endDate: $scope.search.scheduleEnd?moment($scope.search.scheduleEnd).format('X'):null,
        onSelected: function (data, scope) {  //时间选择确认事件
            $scope.search.scheduleStart =data.startDate? moment(new Date(parseFloat(data.startDate) * 1000)).format("YYYY-MM-DD"):null;
            $scope.search.scheduleEnd =data.endDate? moment(new Date(new Date(parseFloat(data.endDate) * 1000))).format("YYYY-MM-DD"):null;
            $state.go($state.current.name, $scope.search)

        }
    };


    $scope.searchClick=function(keyword){
        $scope.search.pageNo=1;
        $scope.search.shortName=keyword;
        $state.go($state.current.name, $scope.search)

    }
    AdContractsManager.adContractsList([$scope.search.pageNo, $scope.search.pageSize], $scope.search).then(function (result) {
        $scope.adContractsList = result.data.pageList;
        $scope.paginationConf.totalItems = result.data.total;

    })

    $scope.searchChange=function () {
        $scope.search.pageNo=1;
        $state.go($state.current.name, $scope.search)
    }


    $scope.deletes = function (key) {
        $scope.search[key]=null;
        $scope.search.pageNo=1;
        $state.go($state.current.name, $scope.search)

    }

    $scope.deleteAd=function () {
        var ids=[];
        $scope.adContractsList.forEach(function (item) {
            if(item.checked){
                ids.push(item.id);
            }
        })
        if(!ids.length){
            return false;
        }
        ImsDialog.open({title: "删除确认", message: "您确定要删除数据吗?"}).then(function (a) {
            if (a == 'ok') {
                AdContractsManager.adContractDelete(ids).then(function (result) {
                    if(result.data.c=="s"){
                        $state.reload();

                    }
                })
            }
        })
        
    }
    
    //排期设置
    $scope.open = function(s) {
        $scope[s] = true;
    };

    $scope.changeDisabled=function(item){
        if(item.disabled==2){
            item.disabled=1;
        }else{
            item.disabled=2;
        }
        AdvertisersManager.campaignsStatusEdit(item.advertiserId,{disabled:disabled}).then(function(result){
            $state.reload();
        })

        /*  CampaignsManager.campaignsStatusEdit(item.campaignId,{status:status}).then(function(result){
         $state.reload();
         })*/
    }
}])

